import insight.miescor.annotations.Code;
import insight.miescor.db.DBManager;
import insight.miescor.opp.domain.Assignment;
import insight.miescor.opp.domain.CodeValue;
import insight.primavera.common.P6helper;
import insight.web.delegates.AjaxJSONDelegate;
import insight.web.delegates.PrimaveraDelegate;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;
import org.sormula.SormulaException;

import com.primavera.ServerException;
import com.primavera.bo.schema.client.UserObs;
import com.primavera.common.value.ObjectId;
import com.primavera.integration.client.EnterpriseLoadManager;
import com.primavera.integration.client.Session;
import com.primavera.integration.client.bo.BOIterator;
import com.primavera.integration.client.bo.BusinessObjectException;
import com.primavera.integration.client.bo.object.Location;
import com.primavera.integration.client.bo.object.OBS;
import com.primavera.integration.client.bo.object.Project;
import com.primavera.integration.client.bo.object.ProjectCode;
import com.primavera.integration.client.bo.object.ProjectCodeType;
import com.primavera.integration.client.bo.object.UserOBS;
import com.primavera.integration.common.rmi.Utils;
import com.primavera.integration.network.NetworkException;

public class TestStub {

	public static class table {
		@Code(name = "Project Name")
		private String prjName;

		@Code(name = "Project ID")
		private String ProjectID;

		public String getPrjName() {
			return prjName;
		}

		public void setPrjName(String prjName) {
			this.prjName = prjName;
		}

		public String getProjectID() {
			return ProjectID;
		}

		public void setProjectID(String projectID) {
			ProjectID = projectID;
		}
	}

	public static void main(String[] args) throws SQLException,
			NoSuchMethodException, SormulaException, BusinessObjectException,
			ServerException, NetworkException {

		// DBManager dbManager = new DBManager("oracle.jdbc.OracleDriver",
		// "jdbc:oracle:thin:@172.17.71.21:1563:dbmcordv", "p6ext",
		// "oracle");
		// List<Assignment> lstass=
		// Assignment.getAssignmentDetailByOppId(dbManager.getDatabase(),"03O14-0008");
		// for(Assignment ass:lstass){
		// System.out.println(ass.getId());
		// }
		// System.exit(0);

		System.setProperty("insight.application.config",
				"F:\\Workspaces\\Miescore\\P6Extensions");
		System.setProperty("primavera.bootstrap.home",
				"F:\\Workspaces\\Miescore\\P6Extensions\\properties");

		P6helper helper = new P6helper();
		setProjectOBS(helper, "02P14-0012");
		// readOBS(helper);
		System.exit(0);

		// Location location = new Location(helper.getSession());
		// location.setName("testLoc1");
		//
		// location.setCountryCode("US");
		//
		// location.setLatitude(0);
		// location.setLongitude(0);
		// ObjectId locId = location.create();

		// Session session=helper.getSession();
		// EnterpriseLoadManager elm = helper.getSession()
		// .getEnterpriseLoadManager();
		// BOIterator<ProjectCode> codeItr = elm.loadProjectCodes(
		// new String[] { "Description","CodeValue" },
		// "CodeValue='Opportunity'", null);
		// while(codeItr.hasNext()){
		// ProjectCode code=codeItr.next();
		// System.out.println(code.getDescription());
		// }

		// PrimaveraDelegate.assignProjectCode(session, project, codeId)

		// String customSql =
		// "Where  Assignment.status in('Pending') and Role in ('PMO') ";
		// Table<Assignment> assTable = dbManager.getDatabase().getTable(
		// Assignment.class);
		// List<Assignment> assignments = assTable.selectAllCustom(customSql);
		// for (Assignment assignment : assignments) {
		// // System.out.println(assignment.getId() + " "
		// // + assignment.getOpp().getUserId());
		// }

		// String insertStoreProc = "{call insertDBUSER(?,?,?,?)}";
		// CallableStatement callableStatement =
		// dbManager.getDatabase().getConnection().prepareCall(insertStoreProc);
		//

		//
		// PreparedStatement statement ;//=
		// dbManager.getDatabase().getConnection().prepareStatement("SELECT * FROM TblTest");
		//
		// // String sql = "INSERT INTO tblTest (id, userId,amt) " +
		// // " VALUES (?, ?,?)";
		// // //statement=
		// dbManager.getDatabase().getConnection().prepareStatement(sql);
		// // Object obj[]=new Object[]{4,"aman",null};
		// //
		// // dbManager.getTemplate().update(sql, obj);
		//
		//
		//
		//
		//
		//
		//
		// statement =
		// dbManager.getDatabase().getConnection().prepareStatement("SELECT * FROM TblTest");
		// ResultSet rs = statement.executeQuery();
		//
		//
		// ResultSetMapper<TblTest> resultSetMapper = new
		// ResultSetMapper<TblTest>();
		//
		//
		//
		// List<TblTest> roleList = resultSetMapper.mapRersultSetToObject1(rs,
		// TblTest.class);
		//
		// for(TblTest role:roleList){
		// System.out.println(role.toString());
		// }
		//
		//
		// TblTest test=new TblTest();
		// test.setId(5);
		// test.setName("ajay");
		//
		// resultSetMapper.buildSqlForInsert(test);
		//
		// System.exit(0);
		//
		//
		// // System.out.println(table.class.isAnnotationPresent(Code.class));
		// // for(Field str:table.class.getDeclaredFields()){
		// // System.out.println(str.getName());
		// // }
		// //
		// // Field[] projectCodeFields =
		// ReflectionDelegate.getAnnotatedFields(table.class, Code.class);
		// // for (Field projectCodeField : projectCodeFields) {
		// //
		// System.out.println(projectCodeField.getAnnotation(Code.class).name());
		// // }
		// // DBManager dbManager = new DBManager("oracle.jdbc.OracleDriver",
		// // "jdbc:oracle:thin:@172.17.71.21:1563:dbmcordv", "p6ext",
		// // "oracle");
		// //
		// // JdbcTemplate template = dbManager.getTemplate();
		// // SqlRowSet rs = template.queryForRowSet("select * from roles");
		// // String[] cols = rs.getMetaData().getColumnNames();
		// // TestStub tStub=new TestStub();
		// //
		// //
		// // while (rs.next()) {
		// //
		// // for (String colName : cols) {
		// //
		// // }
		// // }
	}

	private static void setProjectOBS(P6helper helper, String string)
			throws BusinessObjectException, ServerException, NetworkException {
		Project prj = Project.load(helper.getSession(),
				Project.getMainFields(), new ObjectId(5507));
//		prj.setOBSObjectId(new ObjectId(1874));
//		prj.update();
		Object ob=prj.getOBSName();
		System.out.println(prj.getName() + " OBS -> " + ob);

	}

	private static void readOBS(P6helper helper)
			throws BusinessObjectException, ServerException, NetworkException {
		getOBSList(helper, "Proposal Managers");
		// EnterpriseLoadManager elm = helper.getSession()
		// .getEnterpriseLoadManager();
		// BOIterator<OBS> obsItr = elm.loadOBS(OBS.getAllFields(),
		// "Name='Proposal Managers'", null);
		// while (obsItr.hasNext()) {
		// OBS ob = obsItr.next();
		// System.out.println(ob.getName());
		// BOIterator<UserOBS> userItr = ob.loadUserOBS(
		// UserOBS.getAllFields(), "OBSObjectId='" + ob.getObjectId()
		// + "'", null);
		// while (userItr.hasNext()) {
		// UserOBS uObjs = userItr.next();
		// System.out.println("\t" + uObjs.getUserName()
		// + ",  OBSName -> " + uObjs.getOBSName());
		//
		// }
		//
		// // BOIterator<OBS> childObsItr = ob.loadOBSChildren(
		// // OBS.getAllFields(), null, null);
		// // while (childObsItr.hasNext()) {
		// // OBS ob1 = childObsItr.next();
		// // System.out.println("Child OBS -> " + ob1.getName());
		// // }
		//
		// }
	}

	private static List<CodeValue> getOBSUser(P6helper helper, String OBSName)
			throws BusinessObjectException, ServerException, NetworkException {
		EnterpriseLoadManager elm = helper.getSession()
				.getEnterpriseLoadManager();
		BOIterator<UserOBS> userObsItr = elm.loadUserOBS(new String[] {
				"OBSName", "UserName", "OBSObjectId", "UserObjectId" },
				"OBSName='" + OBSName + "'", null);
		List<CodeValue> userList = new ArrayList<CodeValue>();
		while (userObsItr.hasNext()) {
			UserOBS userOb = userObsItr.next();
			userList.add(new CodeValue(userOb.getUserObjectId().toInteger(),
					userOb.getUserName()));

		}
		return userList;
	}

	private static List<CodeValue> getOBSList(P6helper helper,
			String parentOBSName) throws BusinessObjectException,
			ServerException, NetworkException {
		EnterpriseLoadManager elm = helper.getSession()
				.getEnterpriseLoadManager();
		BOIterator<OBS> obsItr = elm.loadOBS(
				new String[] { "ObjectId", "Name" }, "Name='" + parentOBSName
						+ "'", null);

		List<CodeValue> obsList = new ArrayList<CodeValue>();
		while (obsItr.hasNext()) {
			OBS ob = obsItr.next();
			// System.out.println(ob.getName());
			getChildOBS(helper.getSession(), ob, obsList);
		}
		for (CodeValue v : obsList) {
			System.out.println(v.getValue() + " " + v.getId());
		}
		return null;
	}

	private static void getChildOBS(Session session, OBS obs,
			List<CodeValue> obsList) throws BusinessObjectException,
			ServerException, NetworkException {
		// System.out.println("Adding " + obs.getName());
		obsList.add(new CodeValue(obs.getObjectId().toInteger(), obs.getName()));

		BOIterator<OBS> obsItr = obs.loadOBSChildren(new String[] { "ObjectId",
				"Name" }, null, null);

		while (obsItr.hasNext()) {
			OBS ob = obsItr.next();
			getChildOBS(session, ob, obsList);
		}
	}

	// public static class Table {
	// private List<Row> rows;
	// public Table(){
	//
	// }
	// private void addRow(Row row) {
	// rows.add(row);
	// }
	//
	// public List<Row> getRows() {
	// return rows;
	// }
	//
	// public void setColumns(List<Row> rows) {
	// this.rows = rows;
	// }
	//
	// }
	//
	// public class Row {
	// private List<Column> columns;
	//
	// public void addColumn(Column column) {
	// this.columns.add(column);
	// }
	//
	// public List<Column> getColumns() {
	// return columns;
	// }
	//
	// public void setColumns(List<Column> columns) {
	// this.columns = columns;
	// }
	// }
	//
	// public class Column {
	// @Attribute("colName=firstName")
	// private String colName;
	// private String colValue;
	//
	// public Column(String colName, String colValue) {
	// this.colName = colName;
	// this.colValue = colValue;
	// }
	//
	// public String getColName() {
	// return colName;
	// }
	//
	// public void setColName(String colName) {
	// this.colName = colName;
	// }
	//
	// public String getColValue() {
	// return colValue;
	// }
	//
	// public void setColValue(String colValue) {
	// this.colValue = colValue;
	// }
	// }
}
